package com.example.demo11.mapper;

import com.example.demo11.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface tbstudentMapper {
    // 查询所有学生
    @Select("select * from tb_student")
    List<Student> getAll();

    // 根据 ID 查询学生
    @Select("select * from tb_student where id=#{id}")
    @Results({
            @Result(property = "number", column = "no"),
            @Result(property = "card", column = "cardNo",
                    one = @One(select = "com.example.demo11.mapper.CardMapper.getCardByCardNo"))
    })
    Student getStudentById(Integer id);

    // 动态插入学生信息
    @Insert({"<script>",
            "insert into tb_student ",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">",
            "<if test=\"number != null\">no,</if>",
            "<if test=\"name != null\">name,</if>",
            "<if test=\"sex != null\">sex,</if>",
            "<if test=\"phone != null\">phone,</if>",
            "<if test=\"qq != null\">qq,</if>",
            "</trim>",
            "<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\">",
            "<if test=\"number != null\">#{number},</if>",
            "<if test=\"name != null\">#{name},</if>",
            "<if test=\"sex != null\">#{sex},</if>",
            "<if test=\"phone != null\">#{phone},</if>",
            "<if test=\"qq != null\">#{qq},</if>",
            "</trim>",
            "</script>"})
    @SelectKey(statement = "select last_insert_id()", keyProperty = "id", resultType = Integer.class, before = false)
    int addStudent(Student student);

    // 动态更新学生信息
    @Update({"<script>",
            "update tb_student",
            "<set>",
            "<if test=\"number != null\">no = #{number},</if>",
            "<if test=\"name != null\">name = #{name},</if>",
            "<if test=\"sex != null\">sex = #{sex},</if>",
            "<if test=\"phone != null\">phone = #{phone},</if>",
            "<if test=\"qq != null\">qq = #{qq},</if>",
            "</set>",
            "where id = #{id}",
            "</script>"})
    int updateStudent(Student student);

    // 动态删除学生信息
    @Delete({"<script>",
            "delete from tb_student",
            "where 1=1",
            "<if test=\"id != null\">and id = #{id}</if>",
            "<if test=\"number != null\">and no = #{number}</if>",
            "<if test=\"name != null\">and name = #{name}</if>",
            "</script>"})
    int deleteStudent(Map<String, Object> params);

    // 动态查询学生信息
    @Select({"<script>",
            "select * from tb_student",
            "where 1=1",
            "<if test=\"id != null\">and id = #{id}</if>",
            "<if test=\"number != null\">and no = #{number}</if>",
            "<if test=\"name != null\">and name = #{name}</if>",
            "</script>"})
    @Results({
            @Result(property = "number", column = "no"),
            @Result(property = "card", column = "cardNo",
                    one = @One(select = "com.example.demo11.mapper.CardMapper.getCardByCardNo"))
    })
    List<Student> getStudents(Map<String, Object> params);
}